/*
 * @Author: your name
 * @Date: 2021-09-22 13:37:31
 * @LastEditTime: 2021-09-24 14:22:03
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \product-display\src\utils\request.js
 */
import axios from 'axios'

const defaultHeader = {}

/**
 * @export
 * @param {*} options
 * @returns
 */
export function request (options) {
  return new Promise((resolve, reject) => {
    // 获取授权时 corpId，agentId放在params里
    const instance = axios.create({
      baseURL: process.env.VUE_APP_BASE_URL || '/api',
      timeout: 10000,
      headers: {
        ...defaultHeader,
        ...options.headers
      },
      // 过滤数据
      responseType: options.handleLossOfAccuracy ? 'text' : 'json'
    })
    instance(options).then(response => {
      const res = response.data
      if (res.code === '0' || res.code === '000000') {
        resolve(res.data)
      } else {
        reject(res)
      }
    })
      .catch(error => {
        reject(error)
      })
  })
}
